<template>
  <div class="login">
    <header>
      <h1>
        <router-link to="/">Edu boss管理系统</router-link>
      </h1>
    </header>
    <el-form
      class="login-form"
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      label-position="top"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          class="login-btn"
          type="primary"
          :loading="isLoginLoading"
          @click="onSubmit"
        >登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { Form } from 'element-ui'
import { login } from '@/services/user'

export default Vue.extend({
  name: 'LoginIndex',
  data () {
    return {
      form: {
        phone: '',
        password: ''
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度为6到18位', trigger: 'blur' }
        ]
      },
      isLoginLoading: false
    }
  },
  methods: {
    async onSubmit () {
      try {
        await (this.$refs.form as Form).validate()

        this.isLoginLoading = true

        // const { data } = await request({
        //   method: 'POST',
        //   url: '/front/user/login',
        //   headers: {
        //     'Content-Type': 'application/x-www-form-urlencoded'
        //   },
        //    data: qs.stringify(this.form)
        // })

        const { data } = await login(this.form)

        if (data.state !== 1) {
          this.$message.error(data.message)
        } else {
          // 1. 登录成功，记录登录状态，状态需要能够全局访问（放到 vuex 容器）
          this.$store.commit('setUser', data.content)
          // 2. 返回在访问需要登录的页面的时候判断有没有登录状态（路由拦截器）
          // this.$router.push({
          //   name: 'home'
          // })
          // 登录成功，访问登录之前的页面，redirect为路由守卫传过来的参数
          this.$router.push(this.$route.query.redirect as string || '/')
          this.$message.success('登录成功')
        }
      } catch (err) {
        console.log('登录失败', err)
      }
      this.isLoginLoading = false
    }
  }
})
</script>

<style lang="scss" scoped>
.login {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  header {
    margin-bottom: 20px;
    a {
      margin: 0;
      color: rgba(0,0,0,.5);
      font-size: 3rem;
      font-weight: 300;
      text-decoration: none;
      transition: text-shadow .3s;
    }
  }
  .login-form {
    width: 300px;
    background: #fff;
    padding: 20px;
    border-radius: 5px;
  }
  .login-btn {
    width: 100%;
  }
}
</style>
